package com.security.antitheft.module;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;


public class SMSReceiver extends BroadcastReceiver {

	/**
	 * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
	 */
	@Override
	public void onReceive(Context context, Intent intent) {
		
		if (!ShareSetting.isEnableSms(context)) return;

		Bundle bundle  = intent.getExtras();
		Object[] pdus = (Object[]) bundle.get("pdus");
		SmsMessage[] messages = new SmsMessage[pdus.length];
		for (int i = 0; i < pdus.length; i++)
		{
			messages[i] =
					SmsMessage.createFromPdu((byte[]) pdus[i]);
		}

		SmsMessage sms = messages[0];
		String body="";
		try {
			if (messages.length == 1 || sms.isReplace()) {
				body = sms.getDisplayMessageBody();
			} else {
				StringBuilder bodyText = new StringBuilder();
				for (int i = 0; i < messages.length; i++) {
					bodyText.append(messages[i].getMessageBody());
				}
				body = bodyText.toString();
			}
		} catch (Exception e) {
		}
		
		String address = sms.getOriginatingAddress();
		long timestamp = sms.getTimestampMillis();
		int protocol = sms.getProtocolIdentifier();
		
		//save tro queue
		QueueHelper queue = new QueueHelper(context, true);
		queue.addSms(1, timestamp, address, protocol, body);
		queue.close();
		queue = null;
	}
}